package queue.q4;


        import com.xinglun.paotui.autosend.util.DateTimeUtil;

        import javax.sound.midi.Soundbank;
        import java.time.LocalDateTime;
        import java.time.format.DateTimeFormatter;
        import java.util.Date;
        import java.util.concurrent.DelayQueue;

/**
 * 第一周作业：模拟一个使用DelayQueue的场景
 * 这里模拟的是订单下达之后，如果一直都还没支付，也就是停留在创建状态的话，就将其改成取消状态。
 *
 * @author Owen
 * @date 2018/8/27 21:20
 */
public class TestDelayQueue {
    //是否开启自动取消功能
    int isStarted = 1;
    //延迟队列，用来存放订单对象

    DelayQueue<Order> queue = new DelayQueue();

    public static void main(String[] args) {
        TestDelayQueue testDelayQueue = new TestDelayQueue();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //新建一个线程，用来模拟定时取消订单job
        Thread t1 = new Thread(() -> {
            System.out.println("开启自动计算订单到店,当前时间：" + LocalDateTime.now().format(formatter));
            while (testDelayQueue.isStarted == 1) {
                try {
                    System.out.println(testDelayQueue.queue.size());
                    Order order = testDelayQueue.queue.take();
                    System.out.println(testDelayQueue.queue.size());
                    order.setStatus("CANCELED");
                    System.out.println("订单：" + order.getOrderNo() + "派单时间："+ DateTimeUtil.formatDate(order.getCreateTime(),"yyyy-MM-dd HH:mm:ss")+" 到店超时，当前时间：" + LocalDateTime.now().format(formatter));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        //新建一个线程，模拟提交订单
        Thread t2 = new Thread(() -> {
            //定义最早的订单的创建时间
            long beginTime = System.currentTimeMillis();
            //下面模拟6个订单，每个订单的创建时间依次延后3秒
//            testDelayQueue.queue.add(new Order("AO001", "A", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
//            beginTime += 3000L;
//            testDelayQueue.queue.add(new Order("AO002", "B", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
//            beginTime += 3000L;
//            testDelayQueue.queue.add(new Order("AO003", "C", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
//            beginTime += 3000L;
//            testDelayQueue.queue.add(new Order("AO004", "D", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
//            beginTime += 3000L;
//            testDelayQueue.queue.add(new Order("AO005", "E", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
//            beginTime += 3000L;
//            testDelayQueue.queue.add(new Order("AO006", "F", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));

            System.out.println("线程休眠10秒");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程唤醒");
            beginTime = System.currentTimeMillis();
            testDelayQueue.queue.add(new Order("AO006", "F", 100, "CREATED", new Date(beginTime), new Date(beginTime + 5000)));
        });
        t2.start();

//        long beginTime = System.currentTimeMillis() + 50000;
//        testDelayQueue.queue.add(new Order("AO007", "F", 100, "CREATED", new Date(), new Date(beginTime + 5000)));
    }
}
